package demo2;


import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int val) {
        this.val = val;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] postorder = new int[n];
        int[] inorder = new int[n];
        for (int i = 0; i < n; i++) {
            postorder[i] = scanner.nextInt();
        }
        for (int i = 0; i < n; i++) {
            inorder[i] = scanner.nextInt();
        }

        posPost = n - 1;
        TreeNode root = CreateTree(inorder, postorder, 0, n - 1);
        LeveOrder(root);
    }


    static int posPost;

    public static TreeNode CreateTree(int[] inorder, int[] postorder, int begin, int end) {
        if (begin > end) return null;

        int index = findIndex(inorder,postorder[posPost], begin, end);
        TreeNode root = new TreeNode(inorder[index]);
        posPost--;

        root.right = CreateTree(inorder, postorder, index + 1, end);
        root.left = CreateTree(inorder, postorder, begin, index - 1);
        return root;
    }

    public static int findIndex(int[] inorder, int target, int begin, int end) {
        for (int i = begin; i <= end; i++) {
            if (inorder[i] == target) {
                return i;
            }
        }

        return -1;
    }

    //层序遍历
    public static void LeveOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            TreeNode peek = queue.poll();
            if (peek.left != null) queue.add(peek.left);
            if (peek.right != null) queue.add(peek.right);
            System.out.print(peek.val);
            if(!queue.isEmpty()) System.out.print(" ");
        }
    }
}
